package com.bbex.controller;


import com.bbex.config.sercurity.RedisSessionUser;
import com.bbex.entity.PlatUser;
import com.bbex.pojo.GlobalMessageResponseVo;
import com.bbex.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.util.*;

/**
 * Title :
 * Description : 共享兑换管理
 * Copyright (C) zw.FinTec
 *
 * @author department:研发部
 * @version 修改历史:
 * 修改人 修改日期 修改描述
 * -------------------------------------------<
 */
@Controller
@RequestMapping("/bbuni/draw")
public class RandomDrawController {
    @Autowired
    private PlatUserService platUserService;
    @Autowired
    private RandomDrawService randomDrawService;
    /**
     * HID会员随机抽奖接口接口
     *
     * @return
     */
    @RequestMapping("/getRandomInfo")
    @ResponseBody
    public Mono<GlobalMessageResponseVo> getRandomInfo(String coinId) {
        Mono<SecurityContext> context= ReactiveSecurityContextHolder.getContext();
        return context.filter(c -> Objects.nonNull(c.getAuthentication()))
                .map(s -> s.getAuthentication().getPrincipal())
                .cast(RedisSessionUser.class)
                .map(e -> {
                    String userId=e.getId();
                    String mobile=e.getMobile();
                    String mail=e.getMail();
                    try{
                        PlatUser user= platUserService.findByUserId(userId);
                        if(user !=null){
                            String msg="";
                            msg= randomDrawService.randomDraw(userId,mobile,mail,coinId);
                            return GlobalMessageResponseVo.newSuccessInstance(msg);
                        }else {
                            return GlobalMessageResponseVo.newSuccessInstance("该用户未激活HID");
                        }
                    }catch (Exception E){
                        return GlobalMessageResponseVo.newSuccessInstance("系统或网络异常，抽奖失败");
                    }
                });
    }

    /**
     * HID会员抽奖记录接口
     *
     * @return
     */

    @RequestMapping("/getRandomLog")
    @ResponseBody
    public Mono getRandomLog() {
        List<Map> list = randomDrawService.getRandomLog();
        return Mono.just(GlobalMessageResponseVo.newSuccessInstance(list));
    }


}
